using UnityEngine;
using System.Collections;

public class DrawTiles : MonoBehaviour {
	public int moveSpeed = 140;
    //the texture to create the tiles from
    public Texture2D baseMap;
    //default tile size.  the included texture has 16x16 tiles
    public int tileSize = 16;
    //the tiles
    TileMap tileMap;
    //the tile date telling which tiles to draw where
    int[] tileData;
    //how large is the dungeon **IN TILES**
    //assumes a square dungeon
    public int dungeonWidth = 10;
	public Texture2D character;

    //positioning of the map on screen
    public int screenOffsetX = 20;
    public int screenOffsetY = 20;

    //the offset used to determine which part of the map to show
    public int mapOffsetX = 0;
    public int mapOffsetY = 0;

    Dungeon dungeon;
    FourRoomGenerator generator;

	// Use this for initialization
	void Start () {
        StartCoroutine(CreateTiles());
	}

    IEnumerator CreateTiles()
    {
        tileMap = new TileMap(baseMap, tileSize);

        //random tile data
        tileData = new int[dungeonWidth * dungeonWidth];
        for (int i = 0; i < tileData.Length; i++)
        {
            //yield return null;
            tileData[i] = Random.Range(0, 7);
        }

        dungeon = new Dungeon();
        generator = new FourRoomGenerator();
        dungeon.generator = generator;
        generator.dungeonHeight = dungeonWidth;
        generator.dungeonWidth = dungeonWidth;
        dungeon.NewDungeon();
        tileData = dungeon.tileMap;
		
        yield break;
    }


    // Update is called once per frame
    void Update()
    {
        //move the map around
        if (Input.GetKeyDown(KeyCode.LeftArrow)) mapOffsetX--;
        if (Input.GetKeyDown(KeyCode.RightArrow)) mapOffsetX++;
        if (Input.GetKeyDown(KeyCode.UpArrow)) mapOffsetY--;
        if (Input.GetKeyDown(KeyCode.DownArrow)) mapOffsetY++;
		if (Input.GetKeyDown(KeyCode.N))
		{
		dungeon = new Dungeon();
        generator = new FourRoomGenerator();
        dungeon.generator = generator;
        generator.dungeonHeight = dungeonWidth;
        generator.dungeonWidth = dungeonWidth;
        dungeon.NewDungeon();
        tileData = dungeon.tileMap;
		}
		
	}

    public void OnGUI()
    {
        if (Event.current.type.Equals(EventType.repaint))
        {
            DrawStuff(mapOffsetX, mapOffsetY, 30, 30);
        }

        if (!tileMap.done)
        {
            GUI.Label(new Rect(20, 20, 100, 30), "Creating Tile " + tileMap.counter.ToString());
        }
    }

    public void DrawStuff(int xOffset, int yOffset, int width, int height)
    {
        //check to ensure you're not off map
        if (xOffset + width > dungeonWidth)
        {
            xOffset = dungeonWidth - width;
        }
        if (yOffset + height > dungeonWidth)
        {
            yOffset = dungeonWidth - height;
        }
        xOffset = Mathf.Max(0, xOffset);
        yOffset = Mathf.Max(0, yOffset);

        Rect screenTarget = new Rect();
        //width and height is always the tile size
        screenTarget.width = tileMap.tileSize;
        screenTarget.height = tileMap.tileSize;
        int targetIndex = 0;
		

        for (int row = yOffset; row < yOffset + height; row++)
        {
            for (int col = xOffset; col < xOffset + width; col++)
            {
                screenTarget.x = (col - xOffset) * tileMap.tileSize + screenOffsetX;
                screenTarget.y = (row - yOffset) * tileMap.tileSize + screenOffsetY;
                //targetIndex gives us the position in tileData we need to look at
                //to find which tile to draw.
                targetIndex = col + row * dungeonWidth;
                Graphics.DrawTexture(screenTarget, tileMap.tiles[tileData[targetIndex]]);
				//Graphics.DrawTexture(screenTarget, tileMap.tiles[0]);
				Graphics.DrawTexture(new Rect(125,80,16,16), character);
				
            }
        }
    }
}
